多个Makefile文件编译,Makefile多目标编译和多层次编译

您所在的位置:网站首页 gcc 同时编译多个文件 多个Makefile文件编译,Makefile多目标编译和多层次编译

多个Makefile文件编译,Makefile多目标编译和多层次编译

2023-08-27 08:59| 来源: 网络整理| 查看: 265

多个Makefile文件编译,Makefile多目标编译和多层次编译 READMEMakefile多目标编译Makefile多层次编译a文件夹b文件夹include文件夹obj文件夹 多个Makefile文件编译相关截图make文件夹a_b文件夹project2文件夹

README

文件夹以及文件说明。 根文件夹为make,实现多个Makefile文件编译。

Dir make----- /* 一级文件夹:多Makefile编译 */ | |-------a_b /* 二级文件夹:多目标编译 */ | | | |-------obj /* 三级文件夹:a_b工程目标文件生成的指定位置 */ | | | |------------------main_a.c /* 第一个目标主文件 */ | |------------------main_b.c /* 第二个目标主文件 */ | |------------------test_a.c /* 第一个目标printf功能函数文件 */ | |------------------test_b.c /* 第二个目标printf功能函数文件 */ | |------------------test_a.h /* 第一个目标功能函数声明头文件 */ | |------------------test_b.h /* 第二个目标功能函数声明头文件 */ | |------------------Makefile /* 二级文件夹a_b多目标编译的Makefile文件 */ | |-------projects /* 二级文件夹:多层次编译 */ | | | |-------a /* 三级文件夹:a功能函数实现 */ | | | | | |----------a.c /* a()功能函数实现 */ | | |----------a.h /* a()功能函数声明头文件 */ | | | |-------b /* 三级文件夹: b功能函数实现 */ | | | | | |----------b.c /* b()功能函数实现 */ | | |----------b.h /* b()功能函数声明头文件 */ | | | |-------include /* 三级文件夹:相关常量宏定义头文件 */ | | | | | |----------code.h /* 相关常量宏定义头文件 */ | | | |-------obj /* 三级文件夹:project2工程目标文件生成的指定位置 */ | | | |-------c.c /* c()功能函数实现 */ | |-------c.h /* c()功能函数声明头文件 */ | |-------main.c /* project2工程实现主函数,调用a(),b(),c()函数接口 */ | |-------Makefile /* project2工程实现多层次编译的Makefile */ | | |-------Makefile /* make文件夹实现多个Makefile文件编译,即调用projects和a_b文件夹下Makefile编译 */ !!! 注意: 所有非 main*.c 文件只具有简单的printf功能 所有含有 main*.c 文件只有简单的函数接口调用功能 Makefile多目标编译

在文件夹a_b中实现。 main_a.c: 第一个目标主文件

#include #include #include int main() { print_a(); return 0; }

test_a.c:第一个目标printf功能函数文件

#include void print_a() { printf( "This is test_a.c!\n" ); }

test_a.h:第一个目标功能函数声明头文件

#ifndef _TEST_A_H #define _TEST_A_H #include #include void print_a(); #endif

main_b.c:第二个目标主文件

#include #include #include int main() { print_b(); return 0; }

test_b.c:第二个目标printf功能函数文件

#include void print_b() { printf( "This is test_b.c!\n" ); }

test_b.h:第二个目标功能函数声明头文件

#ifndef _TEST_B_H #define _TEST_B_H #include #include void print_b(); #endif

Makefile:二级文件夹a_b多目标编译的Makefile文件

CC = gcc #选择编译器为gcc CFLAGS = -lm -Wall -g #获取项目根路径 ROOT = $(PWD) #获取当前库函数根路径 INCLUDE = -I$(ROOT) #设置目标生成路径 APP = $(ROOT)/obj #第一目标生成内容 OBJS1 = main_a.o \ test_a.o #第二目标生成内容 OBJS2 = main_b.o \ test_b.o #第一与第二目标输出名字 TARGETS1 = $(APP)/main_a TARGETS2 = $(APP)/main_b #最终目标汇总 TARGETS = $(TARGETS1) TARGETS += $(TARGETS2) #库函数路径汇总 CFLAGS += $(INCLUDE) # 生成目标汇总(包含中间生成的临时目标) OBJS = $(OBJS1) OBJS += $(OBJS2) all:$(TARGETS) #目标1自动编译,包括编译依赖关系自动索引 $(TARGETS1):$(OBJS1) $(CC) -o $(TARGETS1) $(OBJS1) $(CFLAGS) #目标2自动编译,包括编译依赖关系自动索引 $(TARGETS2):$(OBJS2) $(CC) -o $(TARGETS2) $(OBJS2) $(CFLAGS) #清除工程编译内容,全部目标清除 clean: -$(RM) $(OBJS) -$(RM) $(TARGETS) Makefile多层次编译

Makefile文件

ROOT = $(PWD) A = $(ROOT)/a B = $(ROOT)/b INCLUDE = $(ROOT)/include APP = $(ROOT)/obj CFLAGS = -I$(A) CFLAGS += -I$(B) CFLAGS += -I$(ROOT) CFLAGS += -I$(INCLUDE) OBJS = main.o $(A)/a.o $(B)/b.o c.o TARGET = $(APP)/app all:$(TARGET) $(TARGET):$(OBJS) $(CC) -o $(TARGET) $(OBJS) $(CFLAGS) clean: -$(RM) $(OBJS) -$(RM) $(TARGET)

c.c:c()功能函数实现

#include #include #include #include void c(){ printf( "%d: This is c function!\n" , C ); }

c.h:c()功能函数声明头文件

#include void c();

main.c:project2工程实现主函数,调用a(),b(),c()函数接口

#include #include "a.h" #include "b.h" #include "c.h" void main(){ a(); } a文件夹

三级文件夹: a功能函数实现 a.c:a()功能函数实现

#include #include #include "a.h" #include "b.h" #include "c.h" #include void a(){ c(); b(); printf( "%d: This is a function!\n" , A ); printf( "Well done!\n" ); }

a.h:a()功能函数声明头文件

void a(); b文件夹

三级文件夹: b功能函数实现 b.c:b()功能函数实现

#include #include #include "b.h" #include void b(){ printf( "%d: This is b function!\n" , B ); }

b.h:b()功能函数声明头文件

#include void b(); include文件夹

code.h:相关常量宏定义头文件

#include #include #define A 1 #define B 2 #define C 3 obj文件夹

编译前为空文件夹,编译时为project2工程目标文件生成的指定位置

多个Makefile文件编译

Makefile:位于根文件夹make中

ROOT = $(PWD) SUBDIR = $(ROOT)/a_b SUBDIR += $(ROOT)/project2 define make_subdir @for i in $(SUBDIR); do \ ( cd $$i && make $1 ) \ done; endef ALL: $(call make_subdir) clean: $(call make_subdir , clean) 相关截图 make文件夹

make文件夹

a_b文件夹

a_b文件夹

project2文件夹

project2文件夹 /project2/a 文件夹 /project2/a文件夹 /project2/b 文件夹 /project2/b文件夹 /project2/include 文件夹 /project2/include文件夹 /project2/obj 文件夹 /project2/obj文件夹



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3